{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "harmful-cleanup",
   "metadata": {},
   "outputs": [],
   "source": [
    "from model.resnet_sgd_cosineannealing_inference import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "flush-tribe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define input image dimensions for resizing\n",
    "height = 448\n",
    "width = 448\n",
    "\n",
    "# Define model hyperparameters\n",
    "lr = 0.01\n",
    "momentum = 0.9\n",
    "T_0 = 225 # e.g. 899 / 4 (train_dataset_size / batch_size)\n",
    "T_mult = 1\n",
    "epochs = 10\n",
    "batch_size = 4 # For both train and test sets\n",
    "\n",
    "# Define number of layers for the ResNet neural network, select from [18, 34, 50 ,101, 152]\n",
    "num_layers = 18\n",
    "\n",
    "pretrained_weights = True\n",
    "unfreeze_all_layers = 'False' # i.e. Default: 'False', unfreezes last layer only for tuning\n",
    "\n",
    "train_augmentation = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aggressive-master",
   "metadata": {},
   "outputs": [],
   "source": [
    "bucket = None\n",
    "saved_model_path = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "together-mirror",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = \"training_data/dogs/spaniels_vs_not\"\n",
    "model_dir = \"models\"\n",
    "\n",
    "class _args:\n",
    "    image_height = height\n",
    "    image_width = width\n",
    "    train = os.path.join(data_dir, \"train\")\n",
    "    validation = os.path.join(data_dir, \"validation\")\n",
    "    test = os.path.join(data_dir, \"test\")\n",
    "    model_dir = model_dir\n",
    "    batch_size = batch_size\n",
    "    epochs = epochs\n",
    "    lr = lr\n",
    "    momentum = momentum\n",
    "    T_0 = T_0\n",
    "    T_mult = T_mult\n",
    "    num_layers = num_layers\n",
    "    pretrained_weights = pretrained_weights\n",
    "    s3_bucket = bucket\n",
    "    warm_restart = saved_model_path\n",
    "    unfreeze_all_layers = unfreeze_all_layers\n",
    "    train_augmentation = train_augmentation\n",
    "args = _args()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "smooth-connectivity",
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets = create_datasets(args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "verbal-organizer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "----------\n",
      "train Loss: 1.3398; train Acc: 0.7275;\n",
      "validation Loss: 0.9549; validation Acc: 0.8125;\n",
      "epoch: 1; lr: 0.009567727288213004;\n",
      "\n",
      "Epoch 2/10\n",
      "----------\n",
      "train Loss: 1.3463; train Acc: 0.7882;\n",
      "validation Loss: 0.4135; validation Acc: 0.9313;\n",
      "epoch: 2; lr: 0.008345653031794291;\n",
      "\n",
      "Epoch 3/10\n",
      "----------\n",
      "train Loss: 1.1308; train Acc: 0.8000;\n",
      "validation Loss: 0.2102; validation Acc: 0.9437;\n",
      "epoch: 3; lr: 0.006545084971874737;\n",
      "\n",
      "Epoch 4/10\n",
      "----------\n",
      "train Loss: 2.6955; train Acc: 0.7186;\n",
      "validation Loss: 0.1122; validation Acc: 0.9750;\n",
      "epoch: 4; lr: 0.0044773576836617335;\n",
      "\n",
      "Epoch 5/10\n",
      "----------\n",
      "train Loss: 1.2723; train Acc: 0.8176;\n",
      "validation Loss: 0.3607; validation Acc: 0.9313;\n",
      "epoch: 5; lr: 0.0025000000000000014;\n",
      "\n",
      "Epoch 6/10\n",
      "----------\n",
      "train Loss: 1.5240; train Acc: 0.8010;\n",
      "validation Loss: 0.4237; validation Acc: 0.9375;\n",
      "epoch: 6; lr: 0.0009549150281252633;\n",
      "\n",
      "Epoch 7/10\n",
      "----------\n",
      "train Loss: 2.4163; train Acc: 0.7578;\n",
      "validation Loss: 0.4359; validation Acc: 0.9313;\n",
      "epoch: 7; lr: 0.00010926199633097155;\n",
      "\n",
      "Epoch 8/10\n",
      "----------\n",
      "train Loss: 1.7183; train Acc: 0.8010;\n",
      "validation Loss: 1.0064; validation Acc: 0.9000;\n",
      "epoch: 8; lr: 0.009890738003669028;\n",
      "\n",
      "Epoch 9/10\n",
      "----------\n",
      "train Loss: 1.6536; train Acc: 0.8078;\n",
      "validation Loss: 0.5457; validation Acc: 0.9313;\n",
      "epoch: 9; lr: 0.009045084971874737;\n",
      "\n",
      "Epoch 10/10\n",
      "----------\n",
      "train Loss: 1.9106; train Acc: 0.7843;\n",
      "validation Loss: 1.5505; validation Acc: 0.8688;\n",
      "epoch: 10; lr: 0.0075;\n",
      "\n",
      "Training complete in 55m 11s\n",
      "Best validation Acc: 0.975000\n",
      "models\\model.pth\n",
      "\n",
      "Evaluating best weights:\n",
      "--------------------\n",
      "train Loss: 0.4572 Acc: 0.9147\n",
      "train Avg. F1 Score: 0.915;\n",
      "classification_report: \n",
      "              precision    recall  f1-score   support\n",
      "\n",
      " not_spaniel       0.89      0.94      0.92       510\n",
      "     spaniel       0.94      0.89      0.91       510\n",
      "\n",
      "    accuracy                           0.91      1020\n",
      "   macro avg       0.92      0.91      0.91      1020\n",
      "weighted avg       0.92      0.91      0.91      1020\n",
      ";\n",
      "\n",
      "validation Loss: 0.1122 Acc: 0.9750\n",
      "validation Avg. F1 Score: 0.975;\n",
      "classification_report: \n",
      "              precision    recall  f1-score   support\n",
      "\n",
      " not_spaniel       0.99      0.97      0.98       100\n",
      "     spaniel       0.95      0.98      0.97        60\n",
      "\n",
      "    accuracy                           0.97       160\n",
      "   macro avg       0.97      0.98      0.97       160\n",
      "weighted avg       0.98      0.97      0.98       160\n",
      ";\n",
      "\n",
      "test Loss: 0.2561 Acc: 0.9500\n",
      "test Avg. F1 Score: 0.950;\n",
      "classification_report: \n",
      "              precision    recall  f1-score   support\n",
      "\n",
      " not_spaniel       0.95      0.97      0.96       100\n",
      "     spaniel       0.95      0.92      0.93        60\n",
      "\n",
      "    accuracy                           0.95       160\n",
      "   macro avg       0.95      0.94      0.95       160\n",
      "weighted avg       0.95      0.95      0.95       160\n",
      ";\n",
      "\n"
     ]
    }
   ],
   "source": [
    "train(args, datasets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "rocky-washer",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
